define(function(require, exports, module) {
    require("../../zTree_v3/js/jquery.ztree.all-3.5.min.js");
    require("common/umei.dev");
    var menu_form = require("../../tpl/menu_form.html");
    /*
     * var umei_dialog = require("common/umei.dialog"); var ajax =
     * require("common/umei.ajax"); var umei = require("common/umei.form"); var
     * menu_form = require("../../tpl/menu_form.html");
     */
    var setting = {
	view : {
	    addHoverDom : addHoverDom, removeHoverDom : removeHoverDom, selectedMulti : false
	}, data : {
	    simpleData : {
		enable : true
	    }
	}
    };
    function addHoverDom(treeId, treeNode) {
	var node = $("#" + treeNode.tId + "_a");
	// 新增按钮
	if (node.find("span.add").length == 0) {
	    var addBtn = $("<span class='button add opbtn' title='添加在菜单' onfocus='this.blur();'></span>").appendTo(node).click(function() {
		addMenu(treeNode, "新增菜单", null, treeNode.id, "add");

	    });
	}
	// 编辑按钮
	if (node.find("span.edit").length == 0) {
	    var addBtn = $("<span class='button edit opbtn' title='修改菜单' onfocus='this.blur();'></span>").appendTo(node).click(function() {
		editMenu(treeNode);
	    });
	}
	// 删除按钮
	if (node.find("span.remove").length == 0) {
	    var addBtn = $("<span class='button remove opbtn' title='删除菜单' onfocus='this.blur();'></span>").appendTo(node).click(function() {
		removeNode(treeNode);
	    });
	}
    };
    function removeHoverDom(treeId, treeNode) {
	$("#" + treeNode.tId + "_a").find("span.opbtn").remove();
    };
    function selectAll() {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo");
	zTree.setting.edit.editNameSelectAll = $("#selectAll").attr("checked");
    }

    loadZtree();

    function loadZtree() {
	umei.postRequest({
	    async : true, url : "load_menues.do", fun : function(response) {
		var zNodes = response.data;
		var rootNode = {
		    id : 0, pId : -1, name : "菜单管理", open : true
		};
		zNodes.push(rootNode);
		$.fn.zTree.init($("#treeDemo"), setting, zNodes);
	    }
	});
    }

    /**
     * 显示窗口
     */
    function showDialog(treeNode, title, id, pId, type) {
	var content = menu_form;
	var d = dialog({
	    title : title, content : content, drag : true, okValue : '确 定', ok : function() {
		saveMenu(d, treeNode, id, pId, type);
		return false;
	    }, cancelValue : '取消', cancel : function() {}
	});
	d.showModal();
    }

    /**
     * 编辑菜单
     */
    function addMenu(treeNode) {

	/*
	 * if(treeNode.level>=4){ alert("最多只能设置四级菜单"); return; }
	 */
	showDialog(treeNode, "新增菜单", null, treeNode.id, "add");

	var childrenLength = 0;
	if (treeNode.children) {
	    childrenLength = treeNode.children.length;
	}
	var form = $("#tree-form");
	form.find("input[name='sort']").val(childrenLength + 1);

	// 初始化表单
	umei.initForm({
	    formObj : $("#tree-form")
	});
    }

    /**
     * 编辑菜单
     */
    function editMenu(treeNode) {
	showDialog(treeNode, "编辑菜单", treeNode.id, treeNode.pId, "update");
	var form = $("#tree-form");
	var menuType = treeNode.menuType.type;
	form.find("input[name='menuType']").eq(menuType).attr("checked", "checked");
	if (menuType == 0) {
	    $("#tree-form .perm-code").hide();
	} else {
	    $("#tree-form .perm-code").show();
	}
	form.find("input[name='name']").val(treeNode.name);
	form.find("input[name='menuUrl']").val(treeNode.menuUrl);
	form.find("input[name='permissionCode']").val(treeNode.permissionCode);
	form.find("input[name='sort']").val(treeNode.sort);
	// 初始化表单
	umei.initForm({
	    formObj : $("#tree-form")
	});
    }

    function saveMenu(d, treeNode, id, pId, type) {
	var form = $("#tree-form");
	umei.serialize(form);
	var menuType = $.trim(form.find("input[name='menuType']:checked").val());
	var name = $.trim(form.find("input[name='name']").val());
	var menuUrl = $.trim(form.find("input[name='menuUrl']").val());
	var permissionCode = $.trim(form.find("input[name='permissionCode']").val());
	var sort = $.trim(form.find("input[name='sort']").val());
	if (!umei.verification({
	    formObj : $("#tree-form"),
	    type:"inline"
	})) {
	    return;
	}
	// 请求参数
	var data = {
	    id : id, menuTypeValue : menuType, name : name, menuUrl : menuUrl, permissionCode : permissionCode, sort : sort, pId : pId
	}
	umei.postRequest({
	    async : true, url : "save_menue.do", data : data, fun : function(response) {
		var data = response.data;
		var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		if (type == "add") {
		    zTree.addNodes(treeNode, data);
		} else if (type == "update") {
		    treeNode.name = data.name;
		    treeNode.menuType = data.menuType;
		    treeNode.menuUrl = data.menuUrl;
		    treeNode.pId = data.pId;
		    treeNode.sort = data.sort;
		    treeNode.permissionCode = data.permissionCode;
		    zTree.updateNode(treeNode, true);
		}
		d.remove();
	    }
	});
    }

    function removeNode(treeNode) {
	var data = {
	    id : treeNode.id
	}
	umei.confirm("确定要删除菜单么？", function() {
	    umei.postRequest({
		async : true, url : "del_menue.do", data : data, fun : function(response) {
		    var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		    zTree.removeNode(treeNode);
		}
	    });
	});
    }
});